package com.common.utils;

import java.io.IOException;
import java.io.OutputStream;

import jxl.CellView;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.NumberFormats;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class JxlTable{

	private final static JxlTable jxlTable = new JxlTable();
	public static JxlTable getInstance() {
		 return jxlTable;
	}
	public JxlTable() {
		 
	}
	public static OutputStream createTable(String[] body, OutputStream out) {
	WritableWorkbook book;
	try {
		// book = Workbook.createWorkbook(new File(filePath));
		book = Workbook.createWorkbook(out);
		// 创建一个sheet名为"表格"
		WritableSheet sheet = book.createSheet("表格", 0);
		// 设置NO列宽度
		CellView cellView = new CellView();  
		cellView.setAutosize(true);
		Label tempLabel = null;
//		sheet.setColumnView(j, cellView);
		// 循环写入表头内容
//		tempLabel = new Label(0, 0, "结清证明",getHeaderCellStyle());
//		sheet.addCell(tempLabel);
//		sheet.mergeCells(0, 0, 7, 0);
		
		// 表体输出
		int bodyLen = body.length;
		for (int j = 0; j < bodyLen; j++) {
			String[] bodyTempArr = body[j].split("#");
			for (int k = 0; k < bodyTempArr.length; k++) {
				if(j == 0){
					sheet.setColumnView(k, Integer.parseInt(bodyTempArr[k]));
				}else{
					WritableCellFormat tempCellFormat3 = null;
					tempCellFormat3 = getBodyCellStyle();
					if (tempCellFormat3 != null) {
						if (k == 0 || k == (bodyTempArr.length - 1)) {
							tempCellFormat3.setAlignment(Alignment.CENTRE);
						}
					}
					tempLabel = new Label(k, j-1, bodyTempArr[k],tempCellFormat3);
					sheet.addCell(tempLabel);
				}
			}
		}
		book.write();
		book.close();
	} catch (IOException e) {
		System.out.println("EXCEL创建失败！");
		e.printStackTrace();
	} catch (RowsExceededException e) {
		System.out.println("EXCEL单元设置创建失败！");
		e.printStackTrace();
	} catch (WriteException e) {
		System.out.println("EXCEL写入失败！");
		e.printStackTrace();
	}
	return out;
	}
	public static WritableCellFormat getHeaderCellStyle() {
		WritableFont font = new WritableFont(WritableFont.createFont("宋体"), 10,
		WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE);
		WritableCellFormat headerFormat = new WritableCellFormat(
		NumberFormats.TEXT);
		try {
			// 添加字体设置
			headerFormat.setFont(font);
			// 设置表头表格边框样式
			// 整个表格线为粗线、黑色
			headerFormat.setBorder(Border.ALL, BorderLineStyle.THICK,Colour.BLACK);
			// 表头内容水平居中显示
			headerFormat.setAlignment(Alignment.CENTRE);
		} catch (WriteException e) {
			System.out.println("表头单元格样式设置失败！");
		}
		return headerFormat;
	}
	public static WritableCellFormat getBodyCellStyle() {
		WritableFont font = new WritableFont(WritableFont.createFont("宋体"), 10,
		WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE);
		WritableCellFormat bodyFormat = new WritableCellFormat(font);
		try {
			// 设置单元格背景色：表体为白色
			bodyFormat.setBackground(Colour.WHITE);
			// 设置表头表格边框样式
			// 整个表格线为细线、黑色
			bodyFormat.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
			bodyFormat.setAlignment(Alignment.CENTRE);
		} catch (WriteException e) {
			System.out.println("表体单元格样式设置失败！");
		}
		return bodyFormat;
	}
//	 public static void main(String[] args) {
////	  String header = "NO,姓名,性别,年龄";
//		 Map<String,Object> map = new HashMap<String,Object>();
//		  map.put("name", "王遗风");
//		  map.put("phone", "111111");
//		  map.put("address", "恶人谷");
//		  map.put("for", "江湖");
//	  String[] body = new String[7];
//	  body[0] = "客户名称:,"+map.get("name")+",电话:,"+map.get("phone")+",地址:,"+map.get("address")+",所属区域:,"+map.get("for");
//	  body[1] = "拜访记录明细";
//	  body[2] = "序号,开始时间,结束时间,拜访结果,拜访人";
//	  body[3] = "1,欧阳锋,男,68,54";
//	  body[4] = "2,黄药师,男,67";
//	  body[5] = "3,洪七公,男,70";
//	  body[6] = "4,郭靖,男,32";
//	  String filePath = "D:/test.xls";
//	  JxlTable testJxl = JxlTable.getInstance();
//	  OutputStream oa = null; 
//	  OutputStream flag = testJxl.createTable(body,oa, filePath);
////	  if (flag) {
////	   System.out.println("表格创建成功！！");
////	  }
//	 }
}
